using Microsoft.AspNetCore.Mvc;
using RAG.Application.Dtos;
using RAG.Application.Interfaces;
using RAG.Application.Commands;
using RAG.Domain.Interfaces;
using Microsoft.AspNetCore.Authorization; // Added for Authorize attribute

namespace RAG.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
[Authorize] // 添加JWT认证
public class RolesController : ControllerBase
{
    private readonly IAppRoleService _roleService;
    private readonly IDomainEventPublisher _eventPublisher;

    public RolesController(
        IAppRoleService roleService,
        IDomainEventPublisher eventPublisher)
    {
        _roleService = roleService;
        _eventPublisher = eventPublisher;
    }

    /// <summary>
    /// 获取角色列表（分页）
    /// </summary>
    [HttpGet]
    public async Task<IActionResult> GetRoles(
        [FromQuery] int page = 1, 
        [FromQuery] int size = 10, 
        [FromQuery] string? search = null)
    {
        var result = await _roleService.GetRolesAsync(page, size, search);
        return Ok(result);
    }

    /// <summary>
    /// 获取角色详情
    /// </summary>
    [HttpGet("{roleId:guid}")]
    public async Task<IActionResult> GetRole(Guid roleId)
    {
        var result = await _roleService.GetRoleByIdAsync(roleId);
        return Ok(result);
    }

    /// <summary>
    /// 创建角色
    /// </summary>
    [HttpPost]
    public async Task<IActionResult> CreateRole([FromBody] CreateRoleDto dto)
    {
        var result = await _roleService.CreateRoleAsync(dto);
        return Ok(result);
    }

    /// <summary>
    /// 更新角色
    /// </summary>
    [HttpPut("{roleId:guid}")]
    public async Task<IActionResult> UpdateRole(Guid roleId, [FromBody] UpdateRoleDto dto)
    {
        var result = await _roleService.UpdateRoleAsync(roleId, dto);
        return Ok(result);
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    [HttpDelete("{roleId:guid}")]
    public async Task<IActionResult> DeleteRole(Guid roleId)
    {
        var result = await _roleService.DeleteRoleAsync(roleId);
        return Ok(result);
    }

    /// <summary>
    /// 启用角色
    /// </summary>
    [HttpPut("{roleId:guid}/enable")]
    public async Task<IActionResult> EnableRole(Guid roleId)
    {
        var result = await _roleService.EnableRoleAsync(roleId);
        return Ok(result);
    }

    /// <summary>
    /// 禁用角色
    /// </summary>
    [HttpPut("{roleId:guid}/disable")]
    public async Task<IActionResult> DisableRole(Guid roleId)
    {
        var result = await _roleService.DisableRoleAsync(roleId);
        return Ok(result);
    }

    /// <summary>
    /// 获取角色权限
    /// </summary>
    [HttpGet("{roleId:guid}/permissions")]
    public async Task<IActionResult> GetRolePermissions(Guid roleId)
    {
        var result = await _roleService.GetRolePermissionsAsync(roleId);
        return Ok(result);
    }

    /// <summary>
    /// 为角色分配权限
    /// </summary>
    [HttpPost("{roleId:guid}/permissions")]
    public async Task<IActionResult> AssignPermissions(Guid roleId, [FromBody] AssignPermissionsDto dto)
    {
        var result = await _roleService.AssignPermissionsAsync(roleId, dto);
        return Ok(result);
    }
} 